﻿// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.

using System.ComponentModel;
using System.Windows;
using System.Windows.Data;
using System.Windows.Controls;
using System.Windows.Input;

namespace Expression.Samples.Interactivity.Design {

	public class WorkaroundInkCanvas : InkCanvas, INotifyPropertyChanged {
		public WorkaroundInkCanvas() {
			this.StrokeCollected += new InkCanvasStrokeCollectedEventHandler(WorkaroundInkCanvas_StrokeCollected);

			((UIElement)this).PreviewMouseDown += this.WorkaroundInkCanvas_PreviewMouseDown;
			this.MouseLeftButtonDown += this.WorkaroundInkCanvas_MouseLeftButtonDown;
		}

		void WorkaroundInkCanvas_PreviewMouseDown(object sender, MouseButtonEventArgs e) {
			this.Strokes.Clear();
		}

		void WorkaroundInkCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
			this.Strokes.Clear();
		}

		void WorkaroundInkCanvas_StrokeCollected(object sender, InkCanvasStrokeCollectedEventArgs e) {
			if (this.PropertyChanged != null) {
				BindingExpression be = BindingOperations.GetBindingExpression(((WorkaroundInkCanvas)sender), WorkaroundInkCanvas.StrokesProperty);
				be.UpdateSource();
				be.UpdateTarget();
				//this.PropertyChanged(this, new PropertyChangedEventArgs("Strokes"));
			}
		}

		#region INotifyPropertyChanged Members

		public event PropertyChangedEventHandler PropertyChanged;

		#endregion
	}
}
